layui.use(['element', 'table', 'jquery', 'laydate', 'form'], function () {
    var element = layui.element;
    var table = layui.table;
    var laydate = layui.laydate;
    var form = layui.form;
    let code = JSON.parse(sessionStorage.getItem("userInfo")).code;
    $(function () {
        tableRender();
    })

    function tableRender() {
        table.render({
            elem: '#words_table'
            // , height: 312
            ,
            url: LOCALHOSTURL + '/words/getSelfWords' //数据接口
            ,
            method: "get",
            where: {
                "code": code
            },
            xhrFields: {
                withCredentials: true //默认情况下，标准的跨域请求是不会发送cookie的
            },
            headers: {
                "Authorization": $.cookie("token")
            },
            crossDomain: true,
            response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                ,
                statusCode: 200 //规定成功的状态码，默认：0
            },
            page: true //开启分页
            ,
            title: "单词表",
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [
                [ //表头
                    {
                        type: 'checkbox',
                        fixed: 'left'
                    }, {
                    field: 'id',
                    title: 'ID',
                    sort: true
                }, {
                    field: 'english',
                    title: '单词',
                    templet: function (res) {
                        if (res.english == null || res.english == '')
                            return '暂无';
                        else
                            return res.english;
                    }
                }, {
                    field: 'chinese',
                    title: '翻译',
                    templet: function (res) {
                        if (res.chinese == null || res.chinese == '')
                            return '暂无';
                        else
                            return res.chinese;
                    }
                }, {
                    field: 'author',
                    title: '作者',
                    templet: function (res) {
                        if (res.author != null && res.author != "")
                            return res.author;
                        else
                            return '暂无';
                    }
                }, {
                    field: 'createTime',
                    title: '发布时间',
                    templet: function (res) {
                        return res.createTime;
                    }
                }, {
                    field: 'likesCount',
                    title: '点赞数',
                    templet: function (res) {
                        if (res.likesCount == null)
                            return 0;
                        else
                            return res.likesCount;
                    }
                }, {
                    fixed: 'right',
                    title: '操作',
                    toolbar: '#barDemo',
                    width: 150
                }
                ]
            ],
            done: function (res) {
                console.log(res);
            }
        });
    }

    table.on("tool(words_filter)", function (obj) {
        let data = obj.data;
        switch (obj.event) {
            case 'edit':
                layer.open({
                    type: 2,
                    title: "修改单词",
                    area: ['100%', '100%'],
                    content: ["editWords.html"],
                    shadeClose: true, //点击旁边地方自动关闭
                    //动画
                    anim: 0,
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                        //将按钮改成添加
                        body.find("#english").val(data.english);
                        body.find("#chinese").val(data.chinese);
                        body.find("#update").click(function () {
                                let english = body.find("#english").val();
                                let chinese = body.find("#chinese").val();
                                if (english == '') {
                                    layer.msg("英文释义不能为空", function () {
                                        body.find("#english").focus();
                                    })
                                } else if (english != '') {
                                    if (chinese == '') {
                                        layer.msg("中文释义不能为空", function () {
                                            body.find("#chinese").focus();
                                        })
                                    } else {
                                        let wordData = {
                                            "id":data.id,
                                            "english": english,
                                            "chinese": chinese
                                        }
                                        let result = ajaxModel("/words/editWords", "post", wordData);
                                        console.log(result);
                                        if (result.code == 200) {
                                            layer.alert(result.message, {icon: 1});
                                            tableRender();
                                            layer.close(index);

                                        } else
                                            layer.alert(result.message, {icon: 1});
                                    }
                                }
                            }
                        );
                        body.find("#cancel").click(function () {
                            layer.close(index);
                        });
                        iframeWin.layui.form.render();
                    }
                });
                break;
            case 'del':
                layer.confirm("真的删除单词" + data.english + "?", function (index) {
                    // obj.del();
                    let result = myAjax("/words/deleteWords", "get", {id: data.id});
                    alert(result.message);
                    tableRender();
                    layer.close(index);
                });
                break;
        }
    })
});